 /**    
  *******************************************************************************************************   
  * @attention    
  * Copyright (c) 2023, SHANGHAI FUDAN MICROELECTRONICS GROUP CO., LTD.(FUDAN MICROELECTRONICS./ FUDAN MICRO.)    
  * All rights reserved.    
  *    
  * Processor:                   FM33FG0xxA    
  * http:                        http://www.fmdevelopers.com.cn/    
  *    
  * Redistribution and use in source and binary forms, with or without    
  * modification, are permitted provided that the following conditions are met    
  *    
  * 1. Redistributions of source code must retain the above copyright notice,    
  *    this list of conditions and the following disclaimer.    
  *    
  * 2. Redistributions in binary form must reproduce the above copyright notice,    
  *    this list of conditions and the following disclaimer in the documentation    
  *    and/or other materials provided with the distribution.    
  *    
  * 3. Neither the name of the copyright holder nor the names of its contributors    
  *    may be used to endorse or promote products derived from this software    
  *    without specific prior written permission.    
  *    
  * 4. To provide the most up-to-date information, the revision of our documents     
  *    on the World Wide Web will be the most Current. Your printed copy may be      
  *    an earlier revision. To verify you have the latest information avaliable,    
  *    refer to: http://www.fmdevelopers.com.cn/.    
  *    
  * THIS SOFTWARE IS PROVIDED BY FUDAN MICRO "AS IS" AND ANY EXPRESSED:     
  * ORIMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES     
  * OF MERCHANTABILITY NON-INFRINGEMENT AND FITNESS FOR A PARTICULAR PURPOSE    
  * ARE DISCLAIMED.IN NO EVENT SHALL FUDAN MICRO OR ITS CONTRIBUTORS BE LIABLE     
  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL     
  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS     
  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER    
  * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,     
  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISINGIN ANY WAY OUT OF THE     
  * USE OF THIS SOFTWARE, EVEN IF ADVISED OFTHE POSSIBILITY OF SUCH DAMAGE.  
  *
  ******************************************************************************************************* 
  */ 

#ifndef __CANFD_H__
#define __CANFD_H__

#include "fm33fg0xxa_fl.h"

/* BRS配置 */
#define BRS_DISABLE                             0
#define BRS_ENABLE                              1

/* CANFD帧类型 */
#define FL_CANFD_FORMAT_STANDARD_DATA           0
#define FL_CANFD_FORMAT_STANDARD_REMOTE         1
#define FL_CANFD_FORMAT_EXTEND_DATA             2
#define FL_CANFD_FORMAT_EXTEND_REMOTE           3
#define FL_CANFD_FORMAT_FD_STANDARD_DATA        4
#define FL_CANFD_FORMAT_FD_STANDARD_REMOTE      5        /* 不支持 */
#define FL_CANFD_FORMAT_FD_EXTEND_DATA          6
#define FL_CANFD_FORMAT_FD_EXTEND_REMOTE        7        /* 不支持 */

/* 发送类型 */
#define TX_TYPE_TXMB                            0
#define TX_TYPE_COMMONFIFO                      1
#define TX_TYPE_QUEUE                           2

/* 接收类型 */
#define RX_TYPE_RXMB                            0
#define RX_TYPE_RXFIFO                          1
#define RX_TYPE_COMMONFIFO                      2

typedef struct
{
    uint32_t ID;  /*!< 31-IDE 30-RTR 29-THL使能 28:0-ID */

    uint32_t PTR;  /*!< 31:28-DLC 其余-0 */

    uint32_t FDCTR;     /*!< 31:16-PTR 9:8-信息标签 2-FDF 1-BRS 0-ESI */

    union
    {
        uint8_t Data8[64]; /*!< Contains the data to be transmitted. It ranges from 0
                            to 0xFF. */
        uint32_t Data32[16];
    }Data;
    
    uint8_t TxType;  /*!< 发送buffer类型 参考 TX_TYPE_TXMB 等 */
    uint32_t TxNum;    /*!< 发送buffer编号(仅TX_TYPE_TXMB时有效) 参考 FL_CANFD_TXMB0 等*/
    uint32_t OneShot; /* 是否单发(仅TX_TYPE_TXMB时有效) 参考 FL_CANFD_TXMODE_ONE_SHOT 等 */
} CanTxMsg;

extern CanTxMsg TxMessage;               


typedef struct
{
    uint32_t ID;  /*!< 31-IDE 30-RTR 29-THL使能 28:0-ID */

    uint32_t PTR;  /*!< 31:28-DLC 15:0-时标信息 其余-0 */

    uint32_t FDSTS;     /*!< 31:16-PTR 9:8-信息标签 2-FDF 1-BRS 0-ESI */

    union
    {
        uint8_t Data8[64]; /*!< Contains the data to be transmitted. It ranges from 0
                            to 0xFF. */
        uint32_t Data32[16];
    }Data;
} CanRxMsg;
extern CanRxMsg RxMessage;

#define CAN_TIMEOUT   0x5FFFFU

extern uint8_t Data1[8];

typedef struct
{
    uint16_t VCU_202_Checksum;
    uint8_t VCU_202_RollingCounter;
    uint8_t VCU_DriveReady;
    uint8_t VCU_ActualGear;
    uint8_t VCU_ActualGearValid;
} VCU_202_FRM;
extern VCU_202_FRM VCU_202_Msg;

typedef struct
{
    uint16_t GSM_308_Checksum;
    uint8_t GSM_308_RollingCounter;
    uint8_t GSM_ShiftLeverPos;
    uint8_t GSM_ShifterPosInv;
    uint8_t GSM_Fault;
    uint8_t GSM_ParkButtonSts_VD;
    uint8_t GSM_PButtonLongPress;
    uint8_t GSM_ParkButtonSts;
} GSM_308_FRM;
extern GSM_308_FRM GSM_308_Msg;

typedef struct
{
    uint8_t ESC_214_RollingCounter;
    uint8_t ESC_VehicleSpeedValid;
    uint16_t ESC_214_Checksum;
    uint16_t ESC_VehicleSpeed;
} ESC_214_FRM;
extern ESC_214_FRM ESC_214_Msg;

FL_ErrorStatus Can_Init(CANFD_COMMON_Type *CANFDx);
void CAN_SetMsg(CanTxMsg *TxMesg);
void FL_CAN_Frame_Send(uint32_t frame_format, uint8_t BRS, CanTxMsg *TxMesg, CANFD_COMMON_Type *CANFDx);
void FL_CAN_Rx_Read(CANFD_COMMON_Type *CANFDx, uint8_t RxType, uint8_t Num);
void TransmitMsg_0x308(void);
extern uint16_t IF_GetVehicleSpeed(void);
#endif


